﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    /// <summary>
    /// 数据库版本操作类
    /// </summary>
    public class DatabaseVersionDAL
    {
        private const string SQL_MAX_WS_DATABASE_VERSION = "select max(DBV_VERSION) from WS_DATABASE_VERSION";
        private const string SQL_INSERT_WS_DATABASE_VERSION = "insert into WS_DATABASE_VERSION(DBV_VERSION,DBV_UPDATE_TIME) values(@Version,@UpdateTime)";
        private const string SQL_COUNT_WS_DATABASE_VERSION = "select count(*) from WS_DATABASE_VERSION where 1=1";

        private const string SQL_WHERE_VERSION = " and DBV_VERSION=@Version ";

        private const string PARM_UPDATE_TIME = "@UpdateTime";
        private const string PARM_VERSION = "@Version";

        private static DatabaseVersionDAL instance = new DatabaseVersionDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static DatabaseVersionDAL Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 获取最大的版本号
        /// </summary>
        /// <returns></returns>
        public string GetMaxVersion()
        {
            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, SQL_MAX_WS_DATABASE_VERSION, null))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow t = ds.Tables[0].Rows[0];
                    return Convert.ToString(t.ItemArray[0]);
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="trans">数据库事务对象</param>
        /// <param name="model">模型对象</param>
        /// <returns></returns>
        public int Insert(DbTransaction trans, DatabaseVersionModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_UPDATE_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.UpdateTime });
            parms.Add(new MySqlParameter(PARM_VERSION, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Version });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_DATABASE_VERSION, parms.ToArray());
        }
        /// <summary>
        /// 记录是否存在
        /// </summary>
        /// <param name="version">版本号</param>
        /// <returns></returns>
        public bool Exist(string version)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_DATABASE_VERSION);
            sbSQL.Append(SQL_WHERE_VERSION);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_VERSION, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = version });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
    }
}
